﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IrisGenerator.Common.DatabaseModel;
using System.Data.SqlClient;
using IrisGenerator.Modules.SqlServer2005.DataAccess;
using IrisGenerator.Framework.Database;

namespace IrisGenerator.Modules.SqlServer2005.SqlModel
{
    internal class SqlDatabase
    {
        internal static TableCollection GetTables(string connectionString)
        {
            SqlDataReader dr = null;
            try
            {
                dr = SystemProcedures.GetTablesFromModel(connectionString);
                TableCollection returnCollection = null;
                while (dr.Read())
                {
                    string owner = SqlConvert.ToString(dr["TABLE_OWNER"]);
                    if (!string.IsNullOrEmpty(owner) &&
                        owner != "sys" &&
                        owner != "INFORMATION_SCHEMA"
                        )
                    {
                        if (returnCollection == null)
                            returnCollection = new TableCollection();

                        if (SqlConvert.ToString(dr["TABLE_TYPE"]).Equals("TABLE"))
                        {
                            Table table = new Table();
                            table.TabName = SqlConvert.ToString(dr["table_name"]);
                            table.TabOwner = owner;
                            returnCollection.Add(table);
                        }
                        else
                        {
                            //TODO: Implement views
                        }


                    }
                }
                return returnCollection;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (dr != null)
                    dr.Dispose();
            }
        }
    }
}
